package com.atjinggu.jinggumail.order.listener;

import com.atjinggu.jinggumail.common.to.mq.SeckillOrderTo;
import com.atjinggu.jinggumail.order.entity.OrderEntity;
import com.atjinggu.jinggumail.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author jingming
 * @Type OrderSeckillListener.java
 * @Desc
 * @date 2021/3/24 20:14
 */
@RabbitListener(queues = "order-seckill-order-queue")
@Component
public class OrderSeckillListener {
    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void orderClose(SeckillOrderTo entity, Message message, Channel channel) throws IOException {
        System.out.println("秒杀订单---开始准备订单");
        try {
            orderService.orderSeckill(entity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}